<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>

    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
</head>
<body>
<form action="index.html" method="post" class="definewidth m20" id="myForm">
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">资源名称</td>
            <td><input type="text" name="menu_name" value="挂号信息管理"/></td>
        </tr>
        <tr>
            <td class="tableleft">url</td>
            <td><input type="text" name="menu_url" value="query.action"/></td>
        </tr>
        <tr>
            <td class="tableleft">是否有效</td>
            <td>
                <input type="radio"  id="state1" name="menu_state" value="0" checked/> 有效
                <input type="radio" id="state2" name="menu_state" value="1" /> 无效
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button type="button" id="btnUpdate" class="btn btn-primary" >更新</button>&nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>
    String.prototype.GetValue= function(para) {
    let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
    let r = this.substr(this.indexOf("\?")+1).match(reg);
    if (r!=null) return unescape(r[2]); return null;
}
    $(function () {       
		$('#backid').click(function(){
				window.location.href="index.html";
		 });
            //根据地址获取Id,如果null则执行增加，否则执行更新
            let url = document.location.toString();//获取URL
            let menu_id=url.GetValue("menu_id");
            queryById(menu_id);
            function queryById(menu_id) {
                $.ajax({
                    type: "GET",
                    url: "/resource/" + menu_id, //RESTful风格的API定义
                    data: "",
                    success: function (vo) {
                        let obj = vo.obj;
                        let name = obj.menu_name;
                        let state = obj.menu_state;
                        let url = obj.menu_url;
                        $("input[name='menu_name']").val(name);
                        $("input[name='menu_url']").val(url);
                        var menu_state=$("input[name='menu_state']");
                        /*$("input[name='menu_state']").val(state);*/
                        for (var i=0;i<menu_state.length;i++){
                            if(menu_state[i].value==state){
                                menu_state[i].checked='checked';
                            }
                        }
                    }
                })
            }
            $("#btnUpdate").click(function () {
                $.ajax({
                    type: "POST",//这里必须写POST
                    url: "/resource/"+menu_id,
                    data: $("#myForm").serialize()+"&_method=PUT",
                    dataType:"json",
                    success: function(vo){
                        if(vo.code==200) {
                            alert("更新成功！");
                        }else{
                            alert("更新失败！"+vo.msg);
                        }
                    }
                });
            });
        });
</script>